/**
 * Created by xiangyufang on 2016/9/8.
 */

;$(function(){
    $.fn.extend({
        loadPowerCircle: function(data,colorData){
            var self = this;
            self.empty();
            var width = self.width();
            var height = self.height();
            var outerRadius = 100;
            var innerRadius = 50;
            var len = data.length;

            //计算数组的总大小
            var count = 0;
            for(var i=0;i<len;i++){
                count += data[i];
            }
            if(count == 0){
                self.hide();
                $('#circleNull').show();
                $('#circleCNull').hide();
            }else {
                // 插入svg
                var svg = d3.select("#circleChart")
                    .append("svg")
                    .attr("width", width)
                    .attr("height", height);

                // 对数据进行布局
                var pie = d3.pie();
                var piedata = pie(data);

                var colorOrdinal = d3.scaleOrdinal()
                    .domain(d3.range(colorData.length))
                    .range(colorData);

                var arc = d3.arc()
                    .innerRadius(innerRadius)
                    .outerRadius(outerRadius);

                var arcs = svg.selectAll("g")
                    .data(piedata)
                    .enter()
                    .append("g")
                    .attr("transform","translate(" + width*0.4 + "," + 100 + ")");

                arcs.append("path")
                    .attr("fill",function(d,i){
                        return colorOrdinal(i);
                    })
                    .attr("d",function(d){
                        return arc(d);
                    });

                arcs.append("text")
                    .attr("transform",function(d){
                        return "translate(" + arc.centroid(d) + ")";
                    })
                    .attr("text-anchor", "middle")
                    .attr("fill","white")
                    .text(function(d){
                        var val = (d.data/count*100).toFixed(2);
                        return val + "%";
                    });
            }
        }
    })
});

;(function($,window,document,undefined){
    // 获取动态表单提交过来的数据
    var postData = {
        deviceId: function(){
            return $('#JQ_deviceId').val();
        }(),
        eqptType: function(){
            return $('#JQ_eqptType').val();
        }()
    };

    DyyBase.ajax(corrctrlApi.url_device_power(),{eqptId:postData.deviceId},function(dataSet){
        $('#JQ_value').text(dataSet.result.list[0].pipeValveroomName);
        $('#JQ_project_name').text(dataSet.result.list[0].customNumber);
        $('#JQ_type').text(dataSet.result.list[0].cppowerType);
    });

    var $circleNull = $('#circleNull'); //饼状图空
    var $circleChart = $('#circleChart');  //饼状图
    var $circleText = $('#circleCNull');  //饼状图的说明文字
    //$circleNull.show();
    //$circleChart.hide();
    //$circleText.hide();
    DyyBase.ajax(corrctrlApi.url_record_power_chart(), postData, function (dataSet) {
        if(dataSet.bool){
            var lineData= dataSet.result.cppowerCount;
            //阴保电源折线图
            if(lineData){
                // 交流电压
                var powerVol = d3Charts.formart(lineData,{x:'RECORDTIME',y:'OUTPUTVOLTAGE'});
                // 输出电压图表
                if(powerVol.length>0){
                    d3Charts.ordinal({
                        container: 'JQ_power_voltage',
                        color: ['#5faee6'],
                        data: [powerVol],
                        tip: {
                            show: true,
                            xtag: '记录时间：',
                            ytag: '输出电压(V)：'
                        },
                        xAxis: {                         // x轴数据
                            ticks: Infinity
                        },
                        yAxis: {
                            ticks: Infinity,
                            render: function(d){
                                return d.toFixed(2);
                            }
                        },
                        layout: {
                            xtag: '',
                            ytag: '输出电压(V)',
                            margin: {
                                left: 50,
                                right: 35,
                                top: 30,
                                bottom: 5
                            },
                            xgrid: true,
                            ygrid: true
                        }
                    });
                }else{
                    $('#JQ_power_voltage').trigger('clear');
                }

                // 电流
                var powerElc = d3Charts.formart(lineData,{x:'RECORDTIME',y:'OUTPUTCURRENT'});
                // 输出电流图表
                if(powerElc.length > 0){
                    d3Charts.ordinal({
                        container: 'JQ_power_electric',
                        color: ['#fc633a'],
                        data: [powerElc],
                        tip: {
                            show: true,
                            xtag: '记录时间：',
                            ytag: '输出电流(A)：'
                        },
                        xAxis: {
                            ticks: Infinity
                        },
                        yAxis: {
                            ticks: Infinity,
                            render: function(d){
                                return d.toFixed(2);
                            }
                        },
                        layout: {
                            xtag: '',
                            ytag: '输出电流(A)',
                            margin: {
                                left: 50,
                                right: 35,
                                top: 30,
                                bottom: 5
                            },
                            xgrid: true,
                            ygrid: true
                        }
                    });
                }else{
                    $('#JQ_power_electric').trigger('clear');
                }

                // 电位
                var powerPor = d3Charts.formart(lineData,{x:'RECORDTIME',y:'SETTINGPOTENTIAL'});
                // 输出电位图表
                if(powerPor.length > 0){
                    d3Charts.ordinal({
                        container: 'JQ_power_potential',
                        color: ['#27be5f'],
                        data: [powerPor],
                        tip: {
                            show: true,
                            xtag: '记录时间：',
                            ytag: '控制电位(mV)：'
                        },
                        xAxis: {                         // x轴数据
                            ticks: Infinity
                        },
                        yAxis: {                         // x轴数据
                            ticks: Infinity,
                            render: function(d){
                                return d.toFixed(2);
                            }
                        },
                        layout: {
                            xtag: '',
                            ytag: '控制电位(mV)：',
                            margin: {
                                left: 50,
                                right: 35,
                                top: 30,
                                bottom: 20
                            },
                            xgrid: true,
                            ygrid: true
                        }
                    });
                }else{
                    $('#JQ_power_potential').trigger('clear');
                }

            }

            //饼状图
            var cpwData = dataSet.result.cpwStatedCount;
            if(cpwData != '' && cpwData != undefined && cpwData != null){
                $circleNull.hide();
                $circleText.show();
                $circleChart.show();
                var circleData = [];
                var colorCData = [];
                for(var i=0; i<cpwData.length;i++){
                    if(cpwData[i].NUM == null || cpwData[i].NUM == undefined || cpwData[i].NUM == '' || cpwData[i].NUM == 0 || cpwData[i].NUM == '0'){
                    }else{
                        circleData.push(cpwData[i].NUM);
                        if(cpwData[i].TYPE == null || cpwData[i].TYPE == undefined || cpwData[i].TYPE == ''){
                            colorCData.push('#8bcf42');
                        }else if(cpwData[i].TYPE == '设备故障'){
                            colorCData.push('#28a7d9');
                        }else if(cpwData[i].TYPE == '测试停机'){
                            colorCData.push('#b39dd9');
                        }else if(cpwData[i].TYPE == '调试停机'){
                            colorCData.push('#ffc52c');
                        }else if(cpwData[i].TYPE == '正常运行'){
                            colorCData.push('#fd8d26');
                        }else if(cpwData[i].TYPE == '其它'){
                            colorCData.push('#8bcf42');
                        }else{
                            colorCData.push('#8bcf42');
                        }
                    }
                }
                $circleChart.loadPowerCircle(circleData,colorCData);
            }else{
                $circleNull.show();
                $circleChart.hide();
                $circleText.hide();
            }
        }else{
            $circleNull.show();
            $circleChart.hide();
            $circleText.hide();
        }
    });

    //阴保电源图表导出
    $('#JQ_power_chart_export').click(function(){
        var pipeName = '阴保电源';
        var projectName = $('#JQ_project_name').text();
        $(this).EXPT({
            type: 'name',
            name: pipeName + projectName + '运行统计图表',
            yesBtnAct: function(callback,fname){
                callback();
                canvasPdf.exportHtmlPdf({
                    dom: $('#JQ_chart_power_div'),
                    server: DyyBase.root_path + '/comm/downImgPdf.json',
                    param: {
                        fileName: fname,
                        name: projectName + '运行统计图表'
                    }
                });
            }
        });
    });
})(jQuery,window,document);